package cn.wildfire.chat.kit.user;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.HashMap;

import butterknife.BindView;
import cn.wildfire.chat.app.extendsion.classes.FriendRequestFromSource;
import cn.wildfire.chat.app.extendsion.classes.RequestCommonGroupEntity;
import cn.wildfire.chat.app.extendsion.classes.ResponseEntity;
import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

/**
 * @author Lizhong
 * TODO 社交资料
 */
public class SocialDataActivity extends WfcBaseActivity {

    @BindView(R2.id.tv_customize_signature)
    TextView tvCustomizeSignature;
    @BindView(R2.id.tv_signature_content)
    TextView tvSignatureContent;
    @BindView(R2.id.tv_our_common_group_count)
    TextView tvOurCommonGroupCount;
    @BindView(R2.id.tv_source_from)
    TextView tvSourceFrom;

    UserViewModel userViewModel;
    UserInfo firendUserInfo;
    @BindView(R2.id.rl_our_common_group)
    RelativeLayout rlOurCommonGroup;

    @Override
    protected void afterViews() {
        super.afterViews();
        userViewModel = WfcUIKit.getAppScopeViewModel(UserViewModel.class);
        init();
    }

    private void init() {
        Intent intent = getIntent();
        firendUserInfo = intent.getParcelableExtra("userInfo");
        firendUserInfo = ChatManager.Instance().getUserInfo(firendUserInfo.uid, true);
        tvSignatureContent.setText(firendUserInfo.social);
        setFromSource();
        HashMap<String, Object> params = new HashMap<>();
        params.put("frienduid", firendUserInfo.uid);
        params.put("uid", ChatManager.Instance().getUserId());
        OkHttpRequest.getInstance(this).requestAsynJsonObject(ServerUrlClient.CHATTING_TOGETHER, params, new ReqCallBack<Object>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onReqSuccess(Object result) {
                ResponseEntity<JsonObject, String> responseEntity = new ResponseEntity<>(result.toString());
                if (responseEntity.getErrorCode().equals("0")) {
                    Gson gson = new Gson();
                    RequestCommonGroupEntity requestCommonGroupEntity = gson.fromJson(result.toString(), RequestCommonGroupEntity.class);
                    int groupCount = requestCommonGroupEntity.getResult().size();
                    tvOurCommonGroupCount.setText(String.valueOf(groupCount) + getString(R.string.ge));
                    if (groupCount == 0) {
                        rlOurCommonGroup.setOnClickListener(null);
                    } else {
                        rlOurCommonGroup.setOnClickListener(onClickListener);
                    }
                } else {

                }
            }

            @Override
            public void onReqFailed(String errorMsg) {

            }
        }).request();
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_social_data;
    }

    /**
     * 设置来源显示
     */
    private void setFromSource() {
        if (firendUserInfo == null) {
            return;
        }
        String source = "" + getString(R.string.add_friend_from_search);
        switch (firendUserInfo.fromsource) {
            case FriendRequestFromSource.ADD_FROM_GROUP:
                source = "" + getString(R.string.add_friend_from_group);
                break;
            case FriendRequestFromSource.ADD_FROM_QRCODE:
                source = "" + getString(R.string.add_friend_from_qrscan);
                break;
            default:
                break;
        }
        tvSourceFrom.setText(source);
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 3);
        setReplaceTitle(getTitle().toString(), -1);
        setSearchLayoutVisibility(false);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SocialDataActivity.this, OurCommonGroupActivity.class);
            intent.putExtra("userInfo", firendUserInfo);
            startActivity(intent);
        }
    };

}

